const positionView = require('../views/position.art')
const postionModel = require('../models/postion')
const positionListModel = require('../models/positionList')
const positionListView = require('../views/positionList.art')

const BScroll = require('better-scroll')

class Position {
  constructor() {
    this.render()
    this.moveList = []
    this.myMovieIdList = []
    this.newList =[]
  }


  renderer(moveList){
    let positionListHtml = positionView({
      moveList
    })

    $('main').html(positionListHtml)
    
    new BScroll.default($('main').get(0),{
      probeType: 2
    })
  }

  async render() {
    let that = this
    let result = await postionModel.get({})
    
    let re = /w.h/;
    
    for(let i=0;i<result.movieList.length;i++){
      result.movieList[i].img = result.movieList[i].img.replace(re,'128.180')
    }

    this.moveList = result.movieList

    this.renderer(that.moveList)

    let bScroll = new BScroll.default($('main').get(0),{
      probeType: 2
    })

    

    for(let i = result.movieList.length;i<=result.movieList.length+9;i++){
      this.myMovieIdList.push(result.movieIds[i])
    }

    bScroll.on('scrollEnd', async function() {
      if(this.maxScrollY >= this.y){
        console.log(this.maxScrollY)
        let result2 = await positionListModel.get(
          `/api/ajax/moreComingList?token=&movieIds=${that.myMovieIdList[0]}%2C${that.myMovieIdList[1]}%2C${that.myMovieIdList[2]}%2C${that.myMovieIdList[3]}%2C${that.myMovieIdList[4]}%2C${that.myMovieIdList[5]}%2C${that.myMovieIdList[6]}%2C${that.myMovieIdList[7]}%2C${that.myMovieIdList[8]}%2C${that.myMovieIdList[9]}`
        )

        console.log(result2)

        that.myMovieIdList = [];

        that.movieList = that.moveList.concat(result2.coming)

        let re = /w.h/;
     
        for(let i=0;i<that.movieList.length;i++){
          that.movieList[i].img = that.movieList[i].img.replace(re,'128.180')
        }
        

        that.renderer(that.movieList)

      }
    })



    /* // 定义图片对象
    let $imgHead = $('.head img')
    let $imgFoot = $('.foot img')

    // bScroll 是BetterScroll实例，将来可以用来调用API
    let bScroll = new BScroll.default($main.get(0), {
      probeType: 2
    })

    // 开始要隐藏下拉刷新的div
    bScroll.scrollBy(0, -40)

    bScroll.on('scrollEnd', () => {
      console.log(0)
    })

    bScroll.on('scroll', function() {
      if (this.y > 0) {
        $imgHead.addClass('up')
      } 
    })*/
  }
}

new Position()